﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using XCT.Class;
using XCT.Functions;

namespace XCT.Forms
{
    public partial class FormReports : DevExpress.XtraEditors.XtraForm
    {
        public FormReports()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormReports(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }


        #region "Biến"

        clsReports clsRp = new clsReports();
        public ReportDocument rptreport = new ReportDocument();
        public ReportDocument rptSubreport;          
        string strduongdan = clsDungChung.strduongdan;
        string strbaocao = clsDungChung.strbaocao;

        #endregion

        #region "Sự Kiện"

        private void FormReports_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void FormReports_Load(object sender, EventArgs e)
        {   
            DataSet ds = new DataSet();
            
            switch (strbaocao)
            {
                case "Rpt_PhieuTK_CLLH.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.DoLy = clsDungChung.pDoLy;
                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Chungloai = clsDungChung.pChungloai;
                    clsRp.Hoavan = clsDungChung.pHoaVan;
                    clsRp.Loai = clsDungChung.ploai;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_PhieuTK_BTP.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.CongDoan = clsDungChung.pCongDoan;
                    clsRp.Chungloai = clsDungChung.pChungloai;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_BCSanLuong_CLLH.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Loai = clsDungChung.ploai;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_BaoSL_CLLH.rpt":

                    clsRp.Ca = clsDungChung.pCa;
                    clsRp.Tungay = clsDungChung.pTuNgay;
                    clsRp.Denngay = clsDungChung.pDenNgay;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_PhieuTK_EDC.rpt":

                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Mamau = clsDungChung.pMaMau;
                    clsRp.Chitiet = clsDungChung.pMaCT;
                    clsRp.Coso = clsDungChung.pCoso;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_BCSanLuong_EDC.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Loai = clsDungChung.ploai;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;
                case "Rpt_PhieuTK_DBG.rpt":

                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Mamau = clsDungChung.pMaMau;
                    clsRp.Chitiet = clsDungChung.pMaCT;


                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();
                    break;

                case "Rpt_BCSanLuong_DBG.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;
                    clsRp.Loai = clsDungChung.ploai;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;

                case "Rpt_BCSanLuongDongBoTheoNgayTH_BTP.rpt":

                    clsRp.Tungay = clsDungChung.pTuNgay;
                    clsRp.Denngay = clsDungChung.pDenNgay;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;

                case "Rpt_BCSanLuongDongBoTheoTuanKH_BTP.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;


                case "Rpt_BaoSL_BTP.rpt":

                    clsRp.Tungay = clsDungChung.pTuNgay;
                    clsRp.Denngay = clsDungChung.pDenNgay;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;

                case "Rpt_BCSanLuongDongBoTheoNgayTH_EDC.rpt":

                    clsRp.Tungay = clsDungChung.pTuNgay;
                    clsRp.Denngay = clsDungChung.pDenNgay;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;

                case "Rpt_BCSanLuongDongBoTheoTuanKH_EDC.rpt":

                    clsRp.TuanKH = clsDungChung.pTuanKH;
                    clsRp.SoLSX = clsDungChung.pSoLSX;

                    ds = clsRp.GetDataSet(strbaocao);

                    fNapDuLieuReport(strbaocao, ds, false);
                    Open_Report();

                    break;

            }

        }

        #endregion

        #region "Hàm"
                        
        public void fNapDuLieuReport(string strTenReport, DataSet ds, Boolean subreport)
        {
             if (subreport != true)
            {
                rptreport.Load(strduongdan + strTenReport);
                rptreport.SetDataSource(ds.Tables[0]);
            }
            else
            {
                rptSubreport = rptreport.OpenSubreport(strTenReport);
                rptSubreport.SetDataSource(ds.Tables[0]);
            }           
        }

        public void Open_Report()
        {            
            rptViewer.DisplayGroupTree = false;
            rptViewer.ReportSource = rptreport;
            rptreport.Refresh();
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            rptViewer.Refresh();
            this.Refresh();
        }

        
        #endregion

       
    }
}